﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ProcessTable = CentralOffice.Admin.Data.ApprovalProcessDataSet.ApprovalProcessDataTable;
using ProcessRow = CentralOffice.Admin.Data.ApprovalProcessDataSet.ApprovalProcessRow;
using CentralOffice.Admin.Data;
using Husb.Util;
using Husb.Util.Enum;

namespace CentralOffice.WebClient.Admin.Flow
{
    public partial class ApprovalProcessEdit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                SiteUtil.PopulateListBox(ddlApprovalObject, SiteConstants.ApprovalObject, false);
                SiteUtil.PopulateListBox(ddlUrgencyDegree, SiteConstants.Priority, false);

                string processId = HttpContext.Current.Request.QueryString["id"];
                if (String.IsNullOrEmpty(processId))
                {
                }
                SetFields(processId);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string processId = HttpContext.Current.Request.QueryString["id"];
            //String savePath = SiteUtil.GetDocumentFilePath();
            ApprovalProcessDataSet p = GetFields(processId);
            int cnt = CentralOffice.Admin.BusinessActions.ApprovalProcess.Update(p);

            Guid id = (p.ApprovalProcess.Rows[0] as ProcessRow).Id;

            Server.Transfer("ApprovalProcess.aspx");

        }

        private void SetFields(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                ProcessRow p = CentralOffice.Admin.BusinessActions.ApprovalProcess.GetByIdFromCache(new Guid(id));

                txtName.Text = p.Name;
                ddlApprovalObject.SelectedValue = p.ApprovalObject.ToString();
                ddlUrgencyDegree.SelectedValue = p.UrgencyDegree.ToString();

                txtEmp.Value = p.IsExecutorNull() ? "": p.Executor;
                txtEmpReadOnly.Value = p.IsExecutorNull() ? "" : p.Executor;
            }

        }

        private ApprovalProcessDataSet GetFields(string id)
        {
            ProcessRow p = null;
            if (String.IsNullOrEmpty(id))
            {
                p = (new ApprovalProcessDataSet()).ApprovalProcess.NewApprovalProcessRow();
                SiteUtil.PopulateCommonField(p, Guid.NewGuid());
            }
            else
            {
                // update
                p = CentralOffice.Admin.BusinessActions.ApprovalProcess.GetByIdFromCache(new Guid(id));
            }
            p.Name = txtName.Text;
            p.ApprovalObject = EnumUtil.GetInt32(SiteConstants.ApprovalObject, ddlApprovalObject.SelectedItem.Text);
            p.UrgencyDegree = Int32.Parse(ddlUrgencyDegree.SelectedValue);
            p.Executor = txtEmp.Value;

            if (String.IsNullOrEmpty(id))
            {
                p.Table.Rows.Add(p);
            }

            return p.Table.DataSet as ApprovalProcessDataSet;
        }

        protected void tvEmp_Load(object sender, EventArgs e)
        {
            this.tvEmp.Attributes.Add("OnClick", "OnTreeNodeChecked(event)");
        }

        protected void btnShowPopup_Click(object sender, EventArgs e)
        {
            string[] assignedBy = null; //txtApprovedBy.Value.Split(',');
            LinkButton btn = sender as LinkButton;
            if (btn != null)
            {
                assignedBy = txtEmp.Value.Split(',');
            }

            tvEmp.Nodes.Clear();
            //HumanResources.HumanResourcesHelper.PopulateEmployeeTree(tvEmp, true, assignedBy);
            HumanResources.HumanResourcesHelper.PopulateEmployeeTree(tvEmp, true, assignedBy);//null

            updPnlDepartment.Update();
            mdlDetailView.Show();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            // 保存弹出窗口所更新的信息
            string employees = "";
            string employeesId = "";

            if (tvEmp.CheckedNodes.Count > 0)
            {
                foreach (TreeNode node in tvEmp.CheckedNodes)
                {
                    if (!node.Text.StartsWith("["))
                    {
                        employees += node.Text + ",";
                        employeesId += node.Value + ",";
                    }
                }
            }

            txtEmpReadOnly.Value = employees.Trim(',');
            txtEmp.Value = employees.Trim(',');
        }

    }
}